﻿using Model.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xproject.Util;

namespace WeiXinpProAdmin.Controllers
{
    public class CommonBaseController : Controller
    {
        public const string SessionKey = "admin_1-wxbase_user";
        public const string RefUserIdCookieKey = "admin_2-wxbase_refuser";
        public const string WxSmLoginSessionKey = "admin_3-wxbase_WxSmLoginSessionKey";
        public const string SubscribeWXMPKey = "admin_4-wxbase_SubscribeWXMPKey";
        public const string StoreIdCookieKey = "admin_5-wxbase_StoreIdCookieKey";
        private readonly string _currentUserCacheNamePrefix = "admin_WeiXinCurrentUserCache_";
        // GET: CommonBase
        public  bool IsWxBrowser()
        {
           
            string userAgnet = "";
            if (  HttpContext != null && HttpContext.Request.UserAgent != null)
                userAgnet = HttpContext.Request.UserAgent.ToLower().Trim();
            if (userAgnet.Contains("micromessenger"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static string Site = "Dev";
        public static string Domain = CommonHelper.GetAppSettings("Domain");
        private string GetUserCacheName(string openID)
        {
            return _currentUserCacheNamePrefix + openID;
        }
        /// <summary>
        /// 保持一个会话
        /// </summary>
        /// <param name="user"></param>
        /// <param name="openId"></param>
        public void SetSession(AdminUser user,string openId)
        {
            System.Web.HttpContext.Current.Response.Cookies.Remove(SessionKey);          
            HttpCookie cookie = new HttpCookie(SessionKey);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);          
            Session[SessionKey] = openId; //session只缓存openid 
            string cacheName = GetUserCacheName(openId);
            System.Web.HttpRuntime.Cache.Insert(cacheName, user, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);
        }

       /// <summary>
       /// 清除会话
       /// </summary>
        public  void ClearWxShopSession()
        {
            System.Web.HttpContext.Current.Response.Cookies.Remove(SessionKey);
            string openId = Session[SessionKey] as string;
            string cacheName = GetUserCacheName(openId);
            System.Web.HttpRuntime.Cache.Remove(cacheName);
            Session[SessionKey] = null;
        }

        public object GetSession()
        {
            string openId = Session[SessionKey] as string;
            if (string.IsNullOrEmpty(openId))
            {
                return null;
            }
            else
            {
                string cacheName = GetUserCacheName(openId);
                return System.Web.HttpRuntime.Cache[cacheName]  ;
            }
           
        }

        public   string GetDomain
        {
            get
            {
               return  CommonHelper.GetAppSettings("Domain");                
            }
        }
        /// <summary>
        /// 通用 提示信息 不需要任何登录
        /// </summary>
        /// <param name="Notice"></param>
        /// <returns></returns>
        public ActionResult SystemNotice(string Notice)
        {
            ViewBag.Notice = Notice;
            return View();
        }

    

    
    }
}